<template>
    <el-container class="home_container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="exit">退出</el-button>
        </el-header>
        <el-container>
            <!-- 左侧导航栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
              <div class="toggle_button" @click="toggleCollapse" v-text="value"></div>
              <el-menu :default-active="activePath"
              background-color="#333744"
              text-color="#fff"
              unique-opened
              active-text-color="#409EFF"
              :collapse-transition="false"
              router
              :collapse="isCollapse">
              <!-- 一级导航 -->
              <el-submenu :index="item.id.toString()" v-for="item in MenuList" :key="item.id">
                <template slot="title">
                  <i :class="iconObj[item.id]"></i>
                  <span slot="title">{{item.authName}}</span>
                </template>
                <!-- 二级导航 -->
                <el-menu-item :index="'/home/'+subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/home/'+ subitem.path)">
                  <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{subitem.authName}}</span>
                </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
            </el-aside>
            <!-- 右侧内容主体区域 -->
            <el-main>
              <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
  watch: {
    '$route' (to, from) {
      if (to.path === '/welcome') {
        this.activePath = ''
      }
    }
  },
  data () {
    return {
      value: '收起',
      // 是否折叠
      isCollapse: false,
      MenuList: [],
      // 左侧图标对象
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.localStorage.getItem('activePath') || ''
  },
  // 获取左侧导航栏数据
  methods: {
    saveNavState (activePath) {
      window.localStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.MenuList = res.data
    },
    // 左侧导航展开 和 折叠操作
    toggleCollapse () {
      this.value = this.isCollapse ? '收起' : '展开'
      this.isCollapse = !this.isCollapse
    },
    // 退出登录
    exit () {
      this.$confirm('你确定要退出登录嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          window.sessionStorage.removeItem('token')
          this.$message.warning('已退出登录!')
          this.$router.push('/login')
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
    .home_container{
       height: 100%;
    }
    .el-header{
       background-color: #373d41;
       display: flex;
       justify-content: space-between;
       padding-left: 0;
       align-items: center;
       color: #fff;
       font-size: 20px;
       >div{
           display: flex;
           align-items: center;
           >span{
               margin-left: 15px;
           }
       }
    }
    .el-aside{
       background-color: #333744;
       .el-menu{
         border-right: none;
       }
    }
    .el-main{
       background-color: #eaedf1;
    }
    .toggle_button{
         text-align: center;
         cursor: pointer;
         letter-spacing: 1em;
         background-color: #4a5064;
         line-height: 24px;
         color: #fff;
         font-size: 10px;
    }
    .iconfont{
      margin-right: 10px;
    }
</style>
